﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace CoreLims.Models.CoreLims
{
    using static IronPython.Modules._ast;
    using Supplier = CommonSupplier;

    public static class CommonSupplierExtension
    {
        /// <summary>
        /// 根据 供应商ID，查询供应商信息
        /// </summary>
        /// <param name="SupplierId"></param>
        /// <returns></returns>
        public static Supplier getSupplier(this CommonSupplier supplier, CoreLimsContext dbContext, string SupplierId) => 
            dbContext.Supplier.FirstOrDefault(m => m.SupplierId == SupplierId);

        /// <summary>
        /// 根据 供应商代码，获得供应商信息。
        /// </summary>
        /// <param name="SupplierCode"></param>
        /// <param name="SupplierName"></param>
        /// <returns></returns>
        public static List<Supplier> getSupplierByCode(this CommonSupplier supplier, CoreLimsContext dbContext, string SupplierCode) => 
            dbContext.Supplier.Where(m => m.SupplierCode == SupplierCode).OrderBy(x => x.SupplierCode).ToList();

        /// <summary>
        /// 根据 名称 获得供应商信息，允许重名
        /// </summary>
        /// <param name="SupplierName"></param>
        /// <returns></returns>
        public static List<Supplier> getSupplierByName(this CommonSupplier supplier, CoreLimsContext dbContext, string SupplierName) => 
            dbContext.Supplier.Where(m => m.SupplierName.Contains(SupplierName)).OrderBy(x => x.SupplierCode).ToList();

        /// <summary>
        /// 获得所有供应商列表
        /// </summary>
        /// <returns></returns>
        public static List<Supplier> getSupplierList(this CommonSupplier supplier, CoreLimsContext dbContext ) => 
            dbContext.Supplier.OrderBy(x => x.SupplierCode).ToList();

        /// <summary>
        /// 根据 供应商类型，查询供应商信息
        /// </summary>
        /// <param name="SupplierType"></param>
        /// <returns></returns>
        public static List<Supplier> getSupplierByType(this CommonSupplier supplier, CoreLimsContext dbContext, string SupplierType) => 
            dbContext.Supplier.Where(m => m.SupplierType == SupplierType).OrderBy(x => x.SupplierCode).ToList();

        /// <summary>
        /// 添加供应商信息
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public static string AddSupplier(this CommonSupplier supplier, CoreLimsContext dbContext)
        {
            supplier.SupplierId = Common._newId();
            dbContext.Add(supplier);
            dbContext.SaveChanges();
            return "供应商信息添加成功！";
        }

        /// <summary>
        /// 更新供应商信息
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public static string UpdateSupplier(this CommonSupplier supplier, CoreLimsContext dbContext)
        {
            CommonSupplier sl = Common.Supplier.getSupplier(dbContext, supplier.SupplierId);
            sl.Copy(supplier);
            dbContext.Update(sl);
            dbContext.SaveChanges();
            return "供应商信息更新成功！";
        }

        /// <summary>
        /// 删除供应商信息
        /// </summary>
        /// <param name="SupplierId"></param>
        /// <returns></returns>
        public static string DeleteSupplier(this CommonSupplier supplier, CoreLimsContext dbContext, string SupplierId)
        {
            CommonSupplier sl = Common.Supplier.getSupplier(dbContext, SupplierId);
            dbContext.Remove(sl);
            dbContext.SaveChanges();
            return "删除成功！";
        }

        /// <summary>
        /// 复制记录内容，ID不复制
        /// </summary>
        /// <param name="supplier"></param>
        /// <param name="other"></param>
        /// <returns></returns>
        public static CommonSupplier Copy(this CommonSupplier supplier, CommonSupplier other)
        {
            supplier.SupplierCode = other.SupplierCode;
            supplier.SupplierType = other.SupplierType;
            supplier.SupplierName = other.SupplierName;
            supplier.Address1 = other.Address1;
            supplier.Address2 = other.Address2;
            supplier.Nation = other.Nation;
            supplier.Province = other.Province;
            supplier.City = other.City;
            supplier.ZipCode = other.ZipCode;
            supplier.Telephone = other.Telephone;
            supplier.Fax = other.Fax;
            supplier.Contacts = other.Contacts;
            supplier.ContactTelephone = other.ContactTelephone;
            supplier.Email = other.Email;
            supplier.Website = other.Website;
            supplier.Note = other.Note;

            return supplier;
        }

    }
}
